import re
from django.contrib import admin

from api import models

# HTML显示
from django.utils.safestring import mark_safe

# 设备表配置
class DeviceAdmin(admin.ModelAdmin):
    list_display = ["id", "did", "dy_account", "phone_model"]

admin.site.register(models.Device, DeviceAdmin)


# 任务表配置
class TaskAdmin(admin.ModelAdmin):

    #定制直播间地址栏短标题
    def short_address(self, obj):
        # 4 -  # 在抖音，记录美好生活#【☀梦小冉₈¹₅】正在直播，来和我一起支持Ta吧。复制下方链接，打开【抖音】，直接观看直播！ https://v.douyin.com/iSANwV59/ 7@8.com :9pm
        pattern_text = r"#【(.*?)】"
        match_text = re.search(pattern_text, obj.address).group(1)
        pattern_word = r'[\u4e00-\u9fff]+'
        match_word = re.search(pattern_word, match_text).group(0)

        #print(match_word)
        return match_word
    short_address.__name__ = "短标题"

    # 定制直播间评论(换行显示)
    def display_text(self, obj):
        return mark_safe(f"<pre>{obj.text}</pre>")

    display_text.__name__ = "评论"
    #list_display = ["id", "device", "short_address", "text", "display_text", "loop_count", "status"]
    list_display = ["id", "device", "short_address", "display_text", "loop_count", "status"]

admin.site.register(models.Task, TaskAdmin)

